package com.example.administrator.test.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.example.administrator.test.MoreNeedsActivity;
import com.example.administrator.test.PoiSearchActivity;
import com.example.administrator.test.R;
import com.example.administrator.test.login.CodeActivity;

import utils.FixPosition;
import utils.MySkipPoiSearch;
import utils.SendOrder;
import utils.ShowNearCar;

/**
 * Created by Administrator on 2016/11/18.
 */
public class FragmentTruck extends Fragment {
    private TextureMapView mapView;
    private GeoCoder geoCoder;//地理编码检索类
    private TextView mTruckNote,mMoreNeeds,tv_fragment_truck_start,tv_fragment_truck_end;//行程开始、结束定位地点
    private BaiduMap mBaiduMap;
    private String sStartLongitude,sStartLatitude, sEndLongitude, sEndLatitude;//开始、结束地点经纬度  String类型
    private int j=1;//标志位  用于判断是专车还是货运(0:专车 1：货运)
    private int carType=1;//标志位  用于判断是专车还是货运(0:专车 1：货运)  传递车辆类型数据给后台
    private RadioGroup rg_truck;//单选按钮
    private  int rb_id;//被选中RadioButton  id
    private RadioButton rb_truck;
    private TextView tv_truck_money;//预付金额
    private String locPlace,endAdress;//开始、结束地点的名称
    private double dStartLatitude, dStartLongitude, dEndLongitude, dEndLatitude;//开始、结束地点经纬度  double类型
    private String newToken;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        SDKInitializer.initialize(getActivity());
        final View view=inflater.inflate(R.layout.fragment_truck_all,null);
        newToken= CodeActivity.getToken();
        tv_truck_money= (TextView) view.findViewById(R.id.tv_truck_money);
        rg_truck= (RadioGroup) view.findViewById(R.id.rg_truck);
        //获取开始、结束地点经纬度以及开始地点名称的方法
        getTruckSendOrderInformation();
        //当用户选择结束地点后才调用该方法
        if(!sEndLongitude.equals("0.0")&&!sEndLatitude.equals("0.0")){
            clickTruckCarType(rg_truck,view);
        }
        mMoreNeeds= (TextView) view.findViewById(R.id.more_needs);
        mTruckNote= (TextView) view.findViewById(R.id.truck_add_mote);
        geoCoder = GeoCoder.newInstance();//创建地理编码检索实例
        tv_fragment_truck_start= (TextView) view.findViewById(R.id.tv_fragment_truck_start);
        tv_fragment_truck_end= (TextView) view.findViewById(R.id.tv_fragment_truck_end);
        //调用点击结束地点跳转到poiSearchActivity的方法
        MySkipPoiSearch.MySkipPoiSearch(getActivity(),tv_fragment_truck_end);
        Intent intent=getActivity().getIntent();
        //获取结束地点的名称
        endAdress=intent.getStringExtra("endAdress");
        tv_fragment_truck_end.setText(endAdress);
        mapView= (TextureMapView) view.findViewById(R.id.truck_mapview);
        mapView.showZoomControls(false);//不显示缩放控件
        mBaiduMap =mapView.getMap();

        //点击跳转到更多需求的界面
        mMoreNeeds.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), MoreNeedsActivity.class);
                startActivity(intent);
            }
        });
        //点击弹出自定义的备注dialog
        mTruckNote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater1=getLayoutInflater(getArguments());
                View layout=inflater1.inflate(R.layout.activity_note, (ViewGroup) view.findViewById(R.id.dialog1));
                new AlertDialog.Builder(getContext()).setView(layout).show();
            }
        });
        String carType1=carType+"";//车辆类型 int->String
        //调用显示附近车辆的方法
        ShowNearCar.ShowNearCar(mBaiduMap,carType1,sStartLatitude,sStartLongitude,j);
        return view;
    }
    public void getTruckSendOrderInformation(){
        //开始地点经纬度  double型
        dStartLongitude=FixPosition.getStartLongitude();
        dStartLatitude=FixPosition.getStartLatitude();
        //开始地点经纬度  String类型
        sStartLongitude=dStartLongitude+"";
        sStartLatitude=dStartLatitude+"";
        //获取开始地点的名称
        locPlace=FixPosition.getLocPlace();
        //结束地点经纬度  double型
        dEndLongitude =PoiSearchActivity.getLongitude();
        dEndLatitude =PoiSearchActivity.getLatitude();
        //结束地点经纬度  String型
        sEndLongitude=dEndLongitude+"";
        sEndLatitude=dEndLatitude+"";
    }
    public void clickTruckCarType(final RadioGroup rg_truck, final View view){
        rg_truck.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                rb_truck = (RadioButton) view.findViewById(rg_truck.getCheckedRadioButtonId());
                rb_id= rb_truck.getId();
                if(sEndLongitude.equals("0.0")||sEndLatitude.equals("0.0")){
                    Toast.makeText(getActivity(),"请输入结束地点",Toast.LENGTH_SHORT).show();
                    Log.i("ppp","哈哈哈哈啊哈哈哈哈");
                }
                if(rb_id==R.id.rb1_truck){
                    SendOrder.sendOrder(tv_truck_money,newToken,locPlace,sStartLongitude,sStartLatitude,endAdress,sEndLongitude,sEndLatitude,"3");
                }else if(rb_id==R.id.rb2_truck){
                    SendOrder.sendOrder(tv_truck_money,newToken,locPlace,sStartLongitude,sStartLatitude,endAdress,sEndLongitude,sEndLatitude,"4");
                }else if(rb_id==R.id.rb3_truck){
                    SendOrder.sendOrder(tv_truck_money,newToken,locPlace,sStartLongitude,sStartLatitude,endAdress,sEndLongitude,sEndLatitude,"5");
                }else if(rb_id==R.id.rb4_truck){
                    SendOrder.sendOrder(tv_truck_money,newToken,locPlace,sStartLongitude,sStartLatitude,endAdress,sEndLongitude,sEndLatitude,"6");
                }
            }
        });
    }

    @Override
    public void onPause() {
        mapView.onPause();
        super.onPause();
    }

    @Override
    public void onResume() {
        mapView.onResume();
        //实现定位功能
        FixPosition.FixPosition(mapView,mBaiduMap,getContext(),geoCoder,tv_fragment_truck_start);
        super.onResume();
    }

    @Override
    public void onDestroy() {
        //退出时销毁定位
        //关闭定位图层
        mBaiduMap.setMyLocationEnabled(false);
        mapView.onDestroy();
        mapView=null;
        super.onDestroy();
    }
}
